﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LDSP.BRL;
using BYFW.DAL;

namespace TaskPlanEmailGenerator
{
    public class FavoriteCodificationEmailGenerator
    {
        public void Run()
        {
            List<Codification> codifications = new Codification().GetUnImplementedCodification();

            foreach (Codification cod in codifications)
            {
                IList<int> parentIds = new List<int>();
                List<R_EnterpriseCodification> enterpriseCods = new R_EnterpriseCodification().GetListByCodification(cod.ID);
                foreach (R_EnterpriseCodification enterpriseCode in enterpriseCods)
                {
                    GenerateMailForEnterprise(enterpriseCode, cod);

                    if (enterpriseCode.Enterprise.Parent != null
                        && enterpriseCode.Enterprise.Parent.ID != 0
                        && !parentIds.Contains(enterpriseCode.Enterprise.Parent.ID))
                    {//如果该子公司有母公司，且母公司没有收到过此法规的提醒，则提醒母公司的管理员。
                        GenerateMailForParentCompany(enterpriseCode, cod, enterpriseCode.Enterprise.Parent);

                        parentIds.Add(enterpriseCode.Enterprise.Parent.ID);
                    }
                }

                //清空母公司Id列表,防止内存溢出。
                parentIds.Clear();
            }
        }

        /// <summary>
        /// 母公司接受即将实施法规的提醒
        /// </summary>
        /// <param name="enterpriseCode"></param>
        /// <param name="cod"></param>
        /// <param name="parent"></param>
        private void GenerateMailForParentCompany(R_EnterpriseCodification enterpriseCode, Codification cod, Enterprise parent)
        {
            int alertDays = parent.EnterpriseConfig.CodificationActiveAlert;
            List<EnterpriseUser> enterpriseUsers = new EnterpriseUser().GetEnterpriseUserByEnterpriseId(parent.ID);
            foreach (EnterpriseUser user in enterpriseUsers)
            {
                if (user.UserType == EnterpriseUserType.企业管理员
                    && DateTime.Now.AddDays(alertDays).Date == cod.ImplementDT
                    && user.Email != null && !string.IsNullOrEmpty(user.Email.Trim()))
                {//只有当用户有邮箱时，才生成提醒邮件。
                    GenerateMail(cod, user.Email, alertDays);
                }
            }
        }

        /// <summary>
        /// 企业接受自己定义的适用法规的提醒
        /// </summary>
        /// <param name="enterpriseCode"></param>
        /// <param name="cod"></param>
        private void GenerateMailForEnterprise(R_EnterpriseCodification enterpriseCode, Codification cod)
        {
            int alertDays = enterpriseCode.Enterprise.EnterpriseConfig.CodificationActiveAlert;
            List<EnterpriseUser> enterpriseUsers = new EnterpriseUser().GetEnterpriseUserByEnterpriseId(enterpriseCode.EnterpriseID);
            foreach (EnterpriseUser user in enterpriseUsers)
            {
                if (user.UserType == EnterpriseUserType.企业管理员
                    && DateTime.Now.AddDays(alertDays).Date == cod.ImplementDT
                    && user.Email != null && !string.IsNullOrEmpty(user.Email.Trim()))
                {//只有当用户有邮箱时，才生成提醒邮件。
                    GenerateMail(cod, user.Email, alertDays);
                }
            }
        }

        private void GenerateMail(Codification cod, string bcc, int alertDays)
        {
            //用户设置过提醒时间，且该法规目前还没有生效，则表示要提醒
            Email email = new Email();
            string subject = "<<" + cod.Name + ">>将于" + cod.ImplementDT.Year + "年" + cod.ImplementDT.Month + "月" + cod.ImplementDT.Day + "日" + "生效。";
            email.Subject = subject;
            email.Content = subject;
            email.PlanSendDate = cod.ImplementDT.AddDays(alertDays * -1);
            email.Bcc = bcc;
            email.Add();
        }
    }
}
